const app = getApp()
const $api = require('../../utils/api.js').API;
Page({
    data: {
        // latitude:'38.9375',
        // longitude:'117.3571',
        // markers: [{
        //   id: 1,
        //   latitude: '38.9375',
        //   longitude: '117.3571',
        // }],
        avatar:'',
        img1:'/static/img/pub.png',
        submittrue:true,
        uploadtrue:false,
        form:{
          city:'天津',
          money:0,
          imgs:[],
          job:''
        },
        type:1,
        imgUrl:app.globalData.imgUrl,
        imgsShow:true,
        region: ['天津市', '天津市', '全部'],
        money:['面议','5k以下','5k~8k','8k~10k','10k以上'],
        userTrue:false,
        ruleShow:false,
        example:'请填写岗位介绍',
    },
    onLoad:function(t){
        //wx.showLoading()
        this.setData({
            avatar:wx.getStorageSync('avatarUrl')
        })
        if(wx.getStorageSync('formSession')){
          var form = wx.getStorageSync('formSession')
          console.log('23',form)
          this.setData({
            form:form
          })
          if(form.license){
            this.setData({
              img1:this.data.imgUrl+form.license,
            })
          }
        }else{
          wx.setStorageSync('formSession',this.data.form)
        }

        console.log(this.data.form)
        //this.getLocate()
    },
    //获取定位
    // getLocate:function(){
    //   var that = this
    //   wx.getLocation({
    //     type: 'wgs84',
    //     success (res) {
    //       wx.hideLoading()
    //       var markers = that.data.markers
    //       markers[0].latitude = res.latitude
    //       markers[0].longitude = res.longitude
    //       that.setData({
    //         latitude:res.latitude,
    //         longitude:res.longitude,
    //         markers:markers
    //       })
    //     }
    //    })
    // },
    //动态绑定数据
    bindKeyInput:function(e){
      var form = this.data.form
      var name = e.target.dataset.name
      var value = e.detail.value
      form[name] = value
      wx.setStorageSync('formSession',form)
      this.setData({
        form:form
      })
    },
    submit:function(e){
      var key = e.target.dataset.key;
      if(key == 1){
        var sourceType = 'camera'
      }
      if(key == 2){
        var sourceType = 'album'
      }
      var that = this
      if(this.data.type == 1){
        var count = 1;
      }else{
        var count = 9;
      }
      wx.chooseImage({
        count: count, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: [sourceType], // 可以指定来源是相册还是相机，默认二者都有
        success: function (ress) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = ress.tempFilePaths;
          wx.uploadFile({
            url: that.data.imgUrl+'/api/img/upload',      //此处换上你的接口地址
            filePath: tempFilePaths[0],
            name: 'img',
            header: {  
              "Content-Type": "multipart/form-data",
              'accept': 'application/json',  //若有token，此处换上你的token，没有的话省略
              'token':'tymy'
            },
            formData:{
              'type':that.data.type  //其他额外的formdata，可不写
            },
            success: function(res){
                if(that.data.type == 1){
                  var form = that.data.form;
                  form.license = res.data
                  wx.setStorageSync('formSession',form)
                  //console.log(wx.getStorageSync('formSession'))
                  that.setData({
                    img1:that.data.imgUrl+res.data,
                    form:form,
                    uploadtrue:false
                  })
                }
                //机构配图
                if(that.data.type == 2){
                  var form = that.data.form;
                  form.imgs.push(res.data)
                  wx.setStorageSync('formSession',form)
                  that.setData({
                    form:form,
                    uploadtrue:false
                  })
                  if(form.imgs.length >= 5){
                    that.setData({
                      imgsShow:false
                    })
                  }
                }
            },
            fail: function(res){
              wx.showToast({
                title:'上传失败',
                duration:3000,
                mask:true, 
                icon:'none',
              })
              return;
            },
          })
        }
      })
    },
    submit3:function(){
      this.setData({
        uploadtrue:false
      })
    },
    imgupload:function(e){
      this.setData({
        uploadtrue:true,
        type:e.target.dataset.type
    })
    },
    delete:function(e){
      var index = e.target.dataset.id
      var form = this.data.form
      form.imgs.splice(index,1)
      this.setData({
        form:form,
        imgsShow:true
      })
    },
    companyAdd:function(){
      if(!this.data.userTrue){
        wx.showToast({
          title:'请同意用户协议',
          duration:3000,
          mask:true, 
          icon:'none',
        })
        return;
      }
      var form = this.data.form
      var that = this
      form.uid = wx.getStorageSync('uid')
      var message = {'name':'机构名称必填',
                    'contacts':'联系人必填',
                    'phone':'联系电话必填',
                    'job':'招聘岗位必填',
                    'num':'招聘人数必填',
                    'address':'地址必填',
                    //'license':'营业执照必须上传',
                    //'imgs':'店铺照片必须上传',
                  }
      for (var x in message){
        if(!form[x]){
            wx.showToast({
              title:message[x],
              duration:2000,
              mask:true, 
              icon:'none',
            })
            return;
          }
      }
      that.setData({
        submittrue:false
      })
      $api.post('user/companyAdd',form).then(res => {
        //请求成功
        if(res.data.code == 1){
          wx.setStorageSync('role',3)
          wx.removeStorageSync('formSession')
          wx.showToast({
            title: '注册成功!!',//提示文字
            duration:3000,//显示时长
            mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon:'none', //图标，支持"success"、"loading"   
            success:function(){
              setTimeout(function(){
                wx.switchTab({
                  url: '../index/index'
                })
              },3000) //延迟时间 这里是
            }
          })
        }else{
          that.setData({
            submittrue:true
          })
          wx.showToast({
            title: '注册失败!!',//提示文字
            duration:3000,//显示时长
            mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon:'none', //图标，支持"success"、"loading"   
            success:function(){
              setTimeout(function(){
                wx.switchTab({
                  url: '../index/index'
                })
              },3000) //延迟时间 这里是
            }
        })
      }
    })
    },
    bindRegionChange: function (e) {
      let region = e.detail.value
      if(e.detail.value[1] !== '全部'){
        var city = e.detail.value[1]
      }else{
        var city = e.detail.value[0]
      }
      let form = this.data.form
      form.city = city
      form.region = region.join(',');
      wx.setStorageSync('formSession',form)
      this.setData({
        region:region,
        form: form
    })
  },
  bindSelectChange: function(e){
    let name = e.target.dataset.name
    let dataCenter = this.data[name];
    let key = e.detail.value
    let form = this.data.form
    form[name] = key
    wx.setStorageSync('formSession',form)
    this.setData({
        form:form
    })
},
bindAcceptRule:function(e){
  if (e.detail.value.length !== 0) {
    this.setData({
      userTrue:true
    })
  }else{
    this.setData({
      userTrue:false
    })
  }
},
ruleOk:function(){
  this.setData({
    ruleShow:true
  })
},
ruleBack:function(){
  this.setData({
    ruleShow:false
  })
},
onShareAppMessage: function() {
  return {
      title: "天意美业直通车",
      path: "/pages/index/index?puid="+wx.getStorageSync('uid')
  };
},
// chooseView:function(){
//   var that = this
//   wx.chooseLocation({
//     success: function(res) {
//       var markers = that.data.markers
//       markers[0].latitude = res.latitude
//       markers[0].longitude = res.longitude
//       var form = that.data.form
//       form.address = res.address
//       form.latitude = res.latitude
//       form.longitude = res.longitude
//       wx.setStorageSync('formSession',form)
//       that.setData({
//         latitude:res.latitude,
//         longitude:res.longitude,
//         markers:markers,
//         form:form
//       })
//     },
//   })
// },
  exampleShow:function(){
    var form = this.data.form
    form.remark = '岗位要求 1、年龄18—30周岁，形象好，气质佳，身心健康；2、中专以上学历。3、相关设备熟练操作，技术手法标准；4、具有亲和力、沟通能理，具备服务意思与团队精神，有上进心；5、参加过相关职业培训可优先考虑。';
    this.setData({
      form:form
    })
  },
  goCenter:function(){
    wx.navigateTo({
      url: '/pages/selectCity/index'
    })
  },
  goJob:function(){
    wx.navigateTo({
      url: '/pages/login/post/index'
    })
  }
});